<?php
/**
 * @user: leo
 * @date: 2021/6/17
 * @createTime: 16:52
 * @company: 迈酷网络科技 http://www.maicooit.com
 */
namespace app\admin\logic\common;

use app\BaseController;
use app\admin\model\Menu as MenuModel;
use tauthz\facade\Enforcer;

class Auth extends BaseController{
    //权限检查
    public static function enforceLogic($sub, $obj, $act){
        return Enforcer::enforce($sub, $obj, $act);
    }
    //当前策略添加授权规则
    public static function addPolicyLogic($params){
        return Enforcer::addPolicy($params);
    }
    //确定是否存在授权规则
    public static function hasPolicyLogic($params){
        return Enforcer::hasPolicy($params);
    }
    //当前策略移除授权规则
    public static function removePolicyLogic($params){
        return Enforcer::removePolicy($params);
    }
    //为用户添加角色
    public static function addRoleForUserLogic($username,$role,$domain){
        return Enforcer::addRoleForUser($username, $role,$domain);
    }
    //删除指定用户的指定角色
    public static function deleteRoleForUserLogic($username,$role,$domain){
        return Enforcer::deleteRoleForUser($username,$role,$domain);
    }
    //删除用户的所有角色
    public static function deleteRolesForUserLogic($username,$domain){
        return Enforcer::deleteRolesForUser($username,$domain);
    }
    //获取指定用户具有的角色
    public static function getRolesForUserLogic($username,$domain){
        return Enforcer::getRolesForUser($username,$domain);
    }
    //获取具有指定角色的用户
    public static function getUsersForRoleLogic($role,$domain){
        return Enforcer::getUsersForRole($role,$domain);
    }
    //确定指定用户是否具有指定角色
    public static function hasRoleForUserLogic($username,$role,$domain){
        return Enforcer::hasRoleForUser($username,$role,$domain);
    }
    //为用户或角色添加权限
    public static function addPermissionForUserLogic($username,$role,$domain,$class,$permission,$menu_id){
        return Enforcer::addPermissionForUser($username,$role,$domain, $class, $permission,$menu_id);
    }
    //删除用户或角色的指定权限
    public static function deletePermissionForUserLogic($username,$domain,$class,$permission){
        return Enforcer::deletePermissionForUser($username,$domain,$class,$permission);
    }
    //删除用户或角色的所有权限
    public static function deletePermissionsForUserLogic($username){
        return Enforcer::deletePermissionsForUser($username);
    }
    //获取用户或角色的所有权限(支持多角色)
    public static function getPermissionsForUserLogic($username,$role,$domain){
        $MenuModel = new MenuModel();
        //多角色权限
        $resArr = [];
        $roleArr = str2arr($role);
        foreach ($roleArr as $k=>$v){
            //取出角色权限
            $res = Enforcer::getPermissionsForUserInDomain($username,$v,$domain);
            //取出角色的
            foreach ($res as $vo){
                //判断rules表里的最后一个字段
                if(isset($vo[5])){
                    $where['menu_id'] = $vo[5];
                    $res = $MenuModel->field('compname,comppath')->where($where)->find();
                    $vo[5] = $res;
                }
                //取出对应的菜单
                $resArr[] = $vo;
            }
        }
        return $resArr;
    }
    //确定用户是否具有指定权限
    public static function hasPermissionForUserLogic($username,$domain,$class,$permission){
        return Enforcer::hasPermissionForUser($username,$domain,$class,$permission);
    }
    //获取用户具有的隐式角色
    public static function getImplicitRolesForUserLogic($username,$domain){
        return Enforcer::getImplicitRolesForUser($username,$domain);
    }
    //获取用户具有的隐式权限
    public static function getImplicitPermissionsForUserLogic($username,$domain){
        return Enforcer::getImplicitPermissionsForUser($username,$domain);
    }
    //删除角色
    public static function deleteRoleLogic($role){
        return Enforcer::deleteRole($role);
    }
    //删除用户
    public static function deleteUserLogic($username){
        return Enforcer::deleteUser($username);
    }
    //删除权限
    public static function deletePermissionLogic($premission){
        return Enforcer::deletePermission($premission);
    }

}